package com.insigma.business.qrcode.soldier.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.insigma.business.qrcode.soldier.entity.*;
import com.insigma.business.qrcode.soldier.entity.SoldierTpAllDO;
import com.insigma.business.qrcode.soldier.request.SoldierRefresherRequest;
import com.insigma.business.qrcode.soldier.service.CommonDataDictService;
import com.insigma.business.qrcode.soldier.service.IrsService;
import com.insigma.business.qrcode.soldier.service.SchedulerJobExec;
import com.insigma.business.qrcode.soldier.service.ThirdPartyService;
import com.insigma.business.qrcode.soldier.util.SchedulerJob;
import com.insigma.business.qrcode.soldier.util.ThreadPoolManager;
import com.insigma.business.qrcode.soldier.util.TroubleshootLogsUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;


@Component
public class SoldierExtraMaxCountInterfaceRefresher {
    private static Logger LOGGER = TroubleshootLogsUtils.LOGGER;

    @Autowired
    private ThirdPartyService thirdPartyService;
    @Autowired
    private IrsService irsService;

    private String PREFIX = "SoldierExtraInfoRefresher";

    private Integer pageSize = 500;

    private volatile Boolean stopFlag;

    ConcurrentHashMap<String, Future> futureMap = new ConcurrentHashMap<String, Future>();

    @Autowired
    private CommonDataDictService commonDataDictService;

    private String taskModule = "JOB";

    private String taskRecordKey = "MAXCOUNT";


    public void refreshMaxCountInterfaceInfo(SoldierRefresherRequest request) {
        String code = request.getCode();
        LOGGER.info("refreshMaxCountInterfaceInfo:{}", code);
        if ("STOP".equals(code)) {
            LOGGER.info("****************设置STOP****************");
            stopFlag = true;
            return;
        } else {
            stopFlag = false;
        }
        commonDataDictService.append(taskModule, taskRecordKey, "任务开始:" + DateUtil.format(new Date(), "yyyy:MM:dd HH:mm:ss"));

        ScheduledFuture<?> dibao = ThreadPoolManager.getScheduledThreadPoolInstance().scheduleWithFixedDelay(new SchedulerJob(code, "dibao", futureMap, new SchedulerJobExec() {
            @Override
            public Boolean exec(SoldierTpAllDO soldierTpAllDO) {
                List<IrsSubsistenceEntity> irsSubsistenceEntities = irsService.queryDibao(soldierTpAllDO.getIdCardNo());
                if (!CollectionUtils.isEmpty(irsSubsistenceEntities)) {
                    soldierTpAllDO.setSubsistenceAllowanceFlag(true);
                    soldierTpAllDO.setSubsistenceAllowance(JSON.toJSONString(irsSubsistenceEntities));
                    return true;
                } else if (irsSubsistenceEntities != null) {
                    soldierTpAllDO.setSubsistenceAllowanceFlag(false);
                    soldierTpAllDO.setSubsistenceAllowance(null);
                }
                return false;
            }
        }), 0, 12, TimeUnit.HOURS);

        ScheduledFuture<?> dibian = ThreadPoolManager.getScheduledThreadPoolInstance().scheduleWithFixedDelay(new SchedulerJob(code, "dibian", futureMap, new SchedulerJobExec() {
            @Override
            public Boolean exec(SoldierTpAllDO soldierTpAllDO) {
                List<IrsLowSideEntity> irsLowSideEntities = irsService.queryDibian(soldierTpAllDO.getIdCardNo(), soldierTpAllDO.getNickName());
                if (!CollectionUtils.isEmpty(irsLowSideEntities)) {
                    soldierTpAllDO.setLowSideFlag(true);
                    soldierTpAllDO.setLowSide(JSON.toJSONString(irsLowSideEntities));
                    return true;
                } else if (irsLowSideEntities != null) {
                    soldierTpAllDO.setLowSideFlag(false);
                    soldierTpAllDO.setLowSide(null);
                }
                return false;
            }
        }), 0, 12, TimeUnit.HOURS);

        ScheduledFuture<?> tekun = ThreadPoolManager.getScheduledThreadPoolInstance().scheduleWithFixedDelay(new SchedulerJob(code, "tekun", futureMap, new SchedulerJobExec() {
            @Override
            public Boolean exec(SoldierTpAllDO soldierTpAllDO) {
                List<IrsExtremelyPoorEntity> irsExtremelyPoorEntities = irsService.queryTekun(soldierTpAllDO.getIdCardNo(), soldierTpAllDO.getNickName());
                if (!CollectionUtils.isEmpty(irsExtremelyPoorEntities)) {
                    soldierTpAllDO.setExtremelyPoorFlag(true);
                    soldierTpAllDO.setExtremelyPoor(JSON.toJSONString(irsExtremelyPoorEntities));
                    return true;
                } else if (irsExtremelyPoorEntities != null) {
                    soldierTpAllDO.setExtremelyPoorFlag(false);
                    soldierTpAllDO.setExtremelyPoor(null);
                }
                return false;
            }
        }), 0, 12, TimeUnit.HOURS);

        ScheduledFuture<?> qiye = ThreadPoolManager.getScheduledThreadPoolInstance().scheduleWithFixedDelay(new SchedulerJob(code, "qiye", futureMap, new SchedulerJobExec() {
            @Override
            public Boolean exec(SoldierTpAllDO soldierTpAllDO) {

                boolean changed = false;
                if (soldierTpAllDO.isSocialSecurityFlag()) {
                    String socialSecurity = soldierTpAllDO.getSocialSecurity();
                    List<InsuredInfoEntity> insuredInfoEntities = JSONArray.parseArray(socialSecurity, InsuredInfoEntity.class);
                    Collections.sort(insuredInfoEntities, new Comparator<InsuredInfoEntity>() {
                        @Override
                        public int compare(InsuredInfoEntity o1, InsuredInfoEntity o2) {
                            return o2.getSjsbsj().compareTo(o1.getSjsbsj());
                        }
                    });
                    InsuredInfoEntity lastOne = insuredInfoEntities.get(0);
                    String companyName = lastOne.getAab004();
                    List<TaxEnterpriseInfoEntity> taxEnterpriseInfoEntities = thirdPartyService.queryTaxEnterpriseList(null, companyName);
                    if (!CollectionUtils.isEmpty(taxEnterpriseInfoEntities)) {
                        soldierTpAllDO.setJobTaxCompanyFlag(true);
                        soldierTpAllDO.setJobTaxCompany(JSON.toJSONString(taxEnterpriseInfoEntities));
                    }
                    changed = true;
                }

                if (soldierTpAllDO.isShareholderFlag()) {
                    String shareholder = soldierTpAllDO.getShareholder();
                    List<ShareholderEntity> shareholderEntities = JSONArray.parseArray(shareholder, ShareholderEntity.class);
                    ShareholderEntity shareholderEntity = shareholderEntities.get(0);
                    List<TaxEnterpriseInfoEntity> taxEnterpriseInfoEntities = thirdPartyService.queryTaxEnterpriseList(shareholderEntity.getUniscid(), null);
                    if (!CollectionUtils.isEmpty(taxEnterpriseInfoEntities)) {
                        soldierTpAllDO.setShareTaxCompanyFlag(true);
                        soldierTpAllDO.setShareTaxCompany(JSON.toJSONString(taxEnterpriseInfoEntities));
                    }
                    changed = true;
                }
                return changed;
            }
        }), 0, 12, TimeUnit.HOURS);
        futureMap.put("dibao", dibao);
        futureMap.put("dibian", dibian);
        futureMap.put("tekun", tekun);
        futureMap.put("qy", qiye);
    }


}
